const HOST = require('../../config.js').host

let app = getApp()

Page({
  data: {
    isPlay: false,
    musicTitle: '',
    activeTab: 0,
    activeRow: 0,
    tabWidth: 0,
    sliderOffset: 0,
    sliderLeft: 0,
    scenicTabs: []
  },

  /**
   * 分享
   */
  onShareAppMessage() {
    return {
      title: '西湖礼物|礼物说',
      path: '/pages/scenic/scenic'
    }
  },

  clickTab(e) {
    const idx = e.currentTarget.dataset.idx
    const row = e.currentTarget.dataset.row

    this.activeTab(row, idx)
  },

  activeTab(row, idx) {
    this.setData({
      activeTab: idx,
      activeRow: row
    })
    this.stopMusic()
    this.playMusic(row, idx)
  },

  playOrPause() {
    let state = this.data.isPlay
    const curRow = this.data.activeRow
    const curIdx = this.data.activeTab

    if (state) {
      this.pauseMusic()
    } else {
      this.playMusic(curRow, curIdx)
    }
  },

  playMusic(row, idx) {
    let self = this
    const music = this.data.scenicTabs[row][idx].attach
    let musicUrl = music.url
    console.log(musicUrl)
    let musicTitle = music.name

    this.setData({
      isPlay: true
    })

    setTimeout(() => {
      wx.playBackgroundAudio({
        dataUrl: musicUrl,
        success(res) {
          console.log('播放成功', res)
          self.setData({
            musicTitle,
            isPlay: true
          })
        },
        fail(err) {
          console.log('播放失败', err)
          self.setData({
            isPlay: false
          })
        }
      })
    }, 100)
  },

  pauseMusic() {
    this.setData({
      isPlay: false
    })
    wx.pauseBackgroundAudio()
  },

  stopMusic() {
    wx.stopBackgroundAudio();
  },

  preMusic(e) {
    let nextIdx, nextRow
    const curRow = this.data.activeRow
    const curIdx = this.data.activeTab

    if (curIdx === 0) {
      nextIdx = 4
      nextRow = curRow === 0 ? 1 : 0
    } else {
      nextIdx = curIdx - 1
      nextRow = curRow
    }

    this.activeTab(nextRow, nextIdx)
  },

  nextMusic(e) {
    let nextIdx, nextRow
    const curRow = this.data.activeRow
    const curIdx = this.data.activeTab

    if (curIdx === 4) {
      nextIdx = 0
      nextRow = curRow === 0 ? 1 : 0
    } else {
      nextIdx = curIdx + 1
      nextRow = curRow
    }

    this.activeTab(nextRow, nextIdx)
  },

  onLoad(options) {
    // 记录带参数二维码参数
    let qrcode = {};
    if (options.store) {
      qrcode.key = '50948b19-67b8-42ff-b857-dbd35295b89s';
      qrcode.value = options.store;
    } else if (options.location) {
      qrcode.key = '50948b19-67b8-42ff-b857-dbd35295b83s';
      qrcode.value = options.location;
    } else {
      options.key && (qrcode.key = options.key);
      options.value && (qrcode.value = options.value);
    }

    // 将二维码存放于 App 中
    // app.globalData.qrcode = qrcode;

    // 将二维码存放于 storage 中
    if (qrcode.key && qrcode.value) {
      wx.setStorage({
        key: 'qrcode',
        data: qrcode,
        success() {
          console.log('更新二维码参数成功！');
        },
        fail() {
          console.log('更新二维码参数失败！');
        }
      })
    }

    const systemInfo = wx.getSystemInfoSync()
    this.setData({
      tabWidth: systemInfo.windowWidth / 5
    })

    let self = this
    // 监测音乐状态
    wx.onBackgroundAudioPlay(() => {
      console.log('音乐播放')
      self.setData({
        isPlay: true
      })
    })
    wx.onBackgroundAudioStop(() => {
      console.log('音乐停止')
      self.setData({
        isPlay: false
      })
    })
    wx.onBackgroundAudioPause(() => {
      console.log('音乐暂停')
      self.setData({
        isPlay: false
      })
    })

    wx.request({
      url: HOST + 'v1/scenic/queryList',
      method: 'POST',
      data: {
        name: ''
      },
      success: function (res) {
        if (res.data.sysCode === 1) {
          let arr = []

          let arr1 = res.data.data.slice(0, 5)
          let arr2 = res.data.data.slice(5)

          arr.push(arr1)
          arr.push(arr2)

          const musicTitle = arr1[0].attach.name

          self.setData({
            scenicTabs: arr,
            musicTitle
          })
        }
      }
    })
  }
})